package com.qdu.dao;

import com.qdu.entity.Activity;
import com.qdu.util.DatabaseUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *用于访问数据库的类
 * @author 19645
 */
public class ActivityDao {
    
    //获得所有的活动，对应首页的活动页面
    public List<Activity> getAllActivity() {
        
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Activity> list = new ArrayList<>();
        
        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select * from Activity");
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new Activity
                            (   
                        rs.getInt(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getString(4),
                        rs.getString(5),
                        rs.getString(6),
                        rs.getInt(7),
                        rs.getString(8),
                        rs.getString(9),
                        rs.getString(10),
                        rs.getString(11),
                        rs.getString(12),
                        rs.getString(13),
                        rs.getString(14),
                        rs.getString(15)
                            )
                );
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return list;
    }
    
    //根据活动编号。获取活动所有信息。这个页面对应活动详细页面
    public Activity GetActiveInfoById(String activityId){
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement
            ("select id,Auserid,Atype,Aname,Arank,Anumber,AdeadLine,Astatus,AstartTime,AendTime,Aaddress,Atransport,Anote,Aid from Activity where Aid=?");
            ps.setString(1, activityId);
            rs = ps.executeQuery();
            while (rs.next()) {
                return new Activity(
                        rs.getInt(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getString(4),
                        rs.getString(5),
                        rs.getInt(6),
                        rs.getString(7),
                        rs.getString(8),
                        rs.getString(9),
                        rs.getString(10),
                        rs.getString(11),
                        rs.getString(12),
                        rs.getString(13),
                        rs.getString(14)
                        );
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return null;
    }
    
    //查询到包括图片在内的信息
    public Activity GetActiveInfoByIdIncludeCover(String activityId){
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement
            ("select id,Aid,Auserid,Atype,Aname,Arank,Anumber,AdeadLine,Astatus,AstartTime,AendTime,Aaddress,Atransport,Anote,Acover from Activity where Aid=?");
            ps.setString(1, activityId);
            rs = ps.executeQuery();
            while (rs.next()) {
                return new Activity(
                        rs.getInt(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getString(4),
                        rs.getString(5),
                        rs.getString(6),
                        rs.getInt(7),
                        rs.getString(8),
                        rs.getString(9),
                        rs.getString(10),
                        rs.getString(11),
                        rs.getString(12),
                        rs.getString(13),
                        rs.getString(14),
                        rs.getString(15)
                        );
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return null;
    }
    
    //获取推荐活动列表，对应主页面下面的活动推荐页面
    public List<Activity> getRecommendActivity() {
        
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Activity> list = new ArrayList<>();
        
        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select Aid,Aname,Arank,Anumber,Acover from Activity where id=1 or id=2 or id=3");
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new Activity
                            (   rs.getString(1),
                                rs.getString(2), 
                                rs.getString(3), 
                                rs.getInt(4),
                                rs.getString(5)
                            )
                );
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return list;
    }
    
    //根据关键字获取一个列表，对应搜索后出现的页面
    public List<Activity> getActivityByKeyword(String activityName) {
        
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Activity> list = new ArrayList<>();
        
        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select Aid,Aname,Atype,Arank,Anumber,AstartTime,AendTime from Activity where Aname like ?");
            ps.setString(1, "%" + activityName + "%");
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new Activity
                        (
                            rs.getString(1),
                            rs.getString(2), 
                            rs.getString(3), 
                            rs.getString(4),
                            rs.getInt(5),
                            rs.getString(6),
                            rs.getString(7)
                        )
                );
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return list;
    }
    
    //根据关键词查询（包括图片）
     public List<Activity> getActivityByKeywordIncludeCover(String activityName) {
        
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Activity> list = new ArrayList<>();
        
        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select id,Aid,Auserid,Atype,Aname,Arank,Anumber,AdeadLine,Astatus,AstartTime,AendTime,Aaddress,Atransport,Anote,Acover from Activity where Aname like ?");
            ps.setString(1, "%" + activityName + "%");
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new Activity
                        (
                        rs.getInt(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getString(4),
                        rs.getString(5),
                        rs.getString(6),
                        rs.getInt(7),
                        rs.getString(8),
                        rs.getString(9),
                        rs.getString(10),
                        rs.getString(11),
                        rs.getString(12),
                        rs.getString(13),
                        rs.getString(14),
                        rs.getString(15)
                        )
                );
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return list;
    }
    
}
